﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;
using BLL;
using System.IO;
using System.Data;

namespace PMS.Web
{
    public partial class Maintain : ExportFiles
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string role = PageComm.GetUser(this.Response).role;
                if (role != "管理员")
                {
                    btn_AddRole.Visible = false;
                    gv_PMS_ProjectMaintain.Columns[gv_PMS_ProjectMaintain.Columns.Count - 2].Visible = false;
                    gv_PMS_ProjectMaintain.Columns[gv_PMS_ProjectMaintain.Columns.Count - 1].Visible = false;
                }
                string projectID = Request["projectID"];
                if (!string.IsNullOrEmpty(projectID))
                {
                    hid_ProjectID.Value = projectID;
                    ProjectLoad();
                    DataLoad();
                }
            }
        }

        /// <summary>
        /// 加载项目信息
        /// </summary>
        private void ProjectLoad()
        {
            string projectID = hid_ProjectID.Value;
            PMS_ProjectBLL pbll = new PMS_ProjectBLL();
            PMS_Project project = pbll.GetModel(Convert.ToInt32(projectID));
            lab_ProjectID.Text = project.projectID;
            lab_JDEID.Text = project.JDEID;
            lab_Customer.Text = project.customer;
            lab_CustomerTel.Text = project.customerTel;
            lab_ProjectName.Text = project.projectName;
            lab_ProjectType.Text = project.projectType;
            lab_SystemPath.Text = project.systemPath;
            lab_City.Text = project.city;
        }

        /// <summary>
        /// 获取查询条件
        /// </summary>
        /// <returns></returns>
        private PMS_ProjectMaintainQuery GetQuery()
        {
            PMS_ProjectMaintainQuery query = new PMS_ProjectMaintainQuery();
            query.projectID = Convert.ToInt32(hid_ProjectID.Value);
            if (!string.IsNullOrEmpty(txt_FromDate.Text.Trim()))
                query.startDate = Convert.ToDateTime(txt_FromDate.Text.Trim());
            if (!string.IsNullOrEmpty(txt_ToDate.Text.Trim()))
                query.endDate = Convert.ToDateTime(txt_ToDate.Text.Trim());
            return query;
        }

        /// <summary>
        /// 列表绑定
        /// </summary>
        /// <param name="projectID"></param>
        private void DataLoad()
        {
            try
            {
                PMS_ProjectMaintainQuery query = GetQuery();
                PMS_ProjectMaintainBLL bll = new PMS_ProjectMaintainBLL();
                List<PMS_ProjectMaintain> liste = bll.GetList(query);
                this.gv_PMS_ProjectMaintain.DataSource = liste;
                this.gv_PMS_ProjectMaintain.DataBind();
            }
            catch (Exception ex)
            {
            }
        }

        protected void gv_PMS_ProjectMaintain_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            GridViewRow row = e.Row;
            if (row.RowType == DataControlRowType.DataRow)
            {
                PMS_ProjectMaintain model = row.DataItem as PMS_ProjectMaintain;
                string planDate = "";
                string completeDate = "";
                if (model.planDate != null && model.planDate != DateTime.MinValue)
                {
                    planDate = model.planDate.ToString("yyyy-MM-dd");
                }
                if (model.completeDate != null && model.completeDate != DateTime.MinValue)
                {
                    completeDate = model.completeDate.ToString("yyyy-MM-dd");
                }
                bool isEdit = (row.RowIndex == gv_PMS_ProjectMaintain.EditIndex);
                if (isEdit == false)
                {
                    row.Cells[1].Text = planDate;
                    row.Cells[2].Text = completeDate;
                }
                else
                {
                    ((TextBox)row.Cells[1].Controls[0]).Text = planDate;
                    ((TextBox)row.Cells[2].Controls[0]).Text = completeDate;
                }
                ImageButton imgbtn_Attachment = row.FindControl("imgbtn_Attachment") as ImageButton;
                if (!string.IsNullOrEmpty(model.attachment))
                {
                    imgbtn_Attachment.Attributes.Add("onclick", "window.open('" + model.attachment + "');return false;");
                }
                else
                {
                    imgbtn_Attachment.Enabled = false;
                }
            }
        }

        protected void btn_Add_Click(object sender, EventArgs e)
        {
            PMS_ProjectMaintain model = new PMS_ProjectMaintain();
            model.id = Convert.ToInt32(hid_ID.Value);
            model.projectID = hid_ProjectID.Value;
            model.quarter = txt_Quarter.Text;
            try
            {
                if (!string.IsNullOrEmpty(txt_PlanDate.Text))
                {
                    model.planDate = Convert.ToDateTime(txt_PlanDate.Text);
                }
                else
                {
                    this.ClientScript.RegisterStartupScript(GetType(), "msg", "<script>alert('请输入预计日期');</script>");
                    return;
                }
            }
            catch
            {
                this.ClientScript.RegisterStartupScript(GetType(), "msg", "<script>alert('预计日期格式错误');</script>");
                return;
            }
            try
            {
                if (!string.IsNullOrEmpty(txt_CompleteDate.Text))
                {
                    model.completeDate = Convert.ToDateTime(txt_CompleteDate.Text);
                }
                else
                {
                    this.ClientScript.RegisterStartupScript(GetType(), "msg", "<script>alert('请输入完成日期');</script>");
                    return;
                }
            }
            catch
            {
                this.ClientScript.RegisterStartupScript(GetType(), "msg", "<script>alert('完成日期格式错误');</script>");
                return;
            }

            model.description = txt_Description.Text;
            if (!string.IsNullOrEmpty(file_Attachment.FileName))
            {
                model.attachment = lab_ProjectID.Text.Trim() + "/" + model.id + file_Attachment.FileName;
            }
            else if (!string.IsNullOrEmpty(hid_Attachment.Value))
            {
                model.attachment = hid_Attachment.Value;
            }
            PMS_ProjectMaintainBLL bll = new PMS_ProjectMaintainBLL();

            if (!bll.AddUpdate(model))
            {
                this.ClientScript.RegisterStartupScript(GetType(), "msg", "<script>alert('更新失败，请稍后重试');</script>");
                return;
            }
            else
            {
                if (!string.IsNullOrEmpty(file_Attachment.FileName))
                {
                    string path = System.AppDomain.CurrentDomain.BaseDirectory + "Upload\\Maintain\\" + lab_ProjectID.Text.Trim() + "\\";
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    path = path + model.id + file_Attachment.FileName;

                    file_Attachment.SaveAs(path);
                }
                string projectID = hid_ProjectID.Value;
                ClearText();
                DataLoad();
                this.ClientScript.RegisterStartupScript(GetType(), "msg", "<script>showContentDiv(0);</script>");
            }
        }

        public void ClearText()
        {
            hid_ID.Value = "0";
            txt_Description.Text = "";
            txt_CompleteDate.Text = "";
            txt_PlanDate.Text = "";
            txt_Quarter.Text = "";
            hid_Attachment.Value = "";
        }

        /// <summary>
        /// 加载当前记录
        /// </summary>
        /// <param name="id"></param>
        public void LoadEditText(int id)
        {
            PMS_ProjectMaintainBLL bll = new PMS_ProjectMaintainBLL();
            PMS_ProjectMaintain model = bll.GetModel(id);
            hid_ID.Value = model.id.ToString();
            txt_Quarter.Text = model.quarter;
            txt_PlanDate.Text = model.planDate.ToString("yyyy-MM-dd");
            txt_CompleteDate.Text = model.completeDate.ToString("yyyy-MM-dd");
            txt_Description.Text = model.description;
            if (!string.IsNullOrEmpty(model.attachment))
                hid_Attachment.Value = model.attachment.Substring(model.attachment.IndexOf("Upload/Maintain/")).Replace("Upload/Maintain/", "");
        }

        protected void gv_PMS_ProjectMaintain_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Edited")
            {
                if (e.CommandArgument != null)
                    LoadEditText(Convert.ToInt32(e.CommandArgument));
                this.ClientScript.RegisterStartupScript(GetType(), "msg", "<script>showContentDiv(1);</script>");
            }
            else if (e.CommandName == "Del")
            {
                if (e.CommandArgument != null)
                {
                    PMS_ProjectMaintainBLL bll = new PMS_ProjectMaintainBLL();
                    if (bll.Delete(Convert.ToInt32(e.CommandArgument)))
                    {
                        this.ClientScript.RegisterStartupScript(GetType(), "msg", "<script>alert('删除成功');</script>");
                        DataBind();
                    }
                    else
                    {
                        this.ClientScript.RegisterStartupScript(GetType(), "msg", "<script>alert('删除失败');</script>");
                    }
                }
            }
        }

        protected void btn_Search_Click(object sender, EventArgs e)
        {
            DataLoad();
        }

        protected void btn_Export_Click(object sender, EventArgs e)
        {
            PMS_ProjectMaintainQuery query = GetQuery();
            PMS_ProjectMaintainBLL bll = new PMS_ProjectMaintainBLL();
            DataTable dt = bll.GetListTable(query);
            if (dt != null && dt.Rows.Count > 0)
            {
                ExportExcel("维保项目清单", dt);
            }
        }
    }
}